﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PInitiatives.Models
{
    public partial class Category
    {
        public Category(string name, int? parent, int left, int right)
        {
            this.Name = name;
            this.Parent = parent;
            this.Left = left;
            this.Right = right;
        }
        
        public int CountInitaitvesInBranch
        {
            get 
            {
                return GetAllInitativesInBranch().Count();
            }
        }

        public IQueryable<Initiative> GetAllInitativesInBranch()
        { 
            PInitiativesEntities db = new PInitiativesEntities();


            return from c in db.Categories
                   from i in db.Initiatives
                   where c.Left >= this.Left && c.Right <= this.Right
                   where i.Category == c.ID
                   select i;

            /*return db.Categories.Where(c => (c.Left >= this.Left && c.Right <= this.Right))
                .Join(db.Initiatives, c => c.ID, i => i.Category, (c, i) =>i);*/
        }

        //public void CountInitiativesOwn()
        //{
        //    if (ID == 0) return;
        //    categoryBranchInitiatives += Initiatives.Count;
        //    ParentCategory.CountInitiativesParent(Initiatives.Count);
        //}

        //public void CountInitiativesParent(int ic)
        //{
        //    categoryBranchInitiatives += ic;
        //    if (ID == 0) return;
        //    ParentCategory.CountInitiativesParent(ic);
        //}



    }
}